package com.jy.adscm.operation.aftersales.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.aftersales.bean.QueryShylMaterial;
import com.jy.adscm.operation.aftersales.bean.ShylMaterial;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShylMaterialMapper;
import com.jy.adscm.operation.aftersales.service.ShylMaterialService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.cg.bean.CgMaterial;
import com.jy.adscm.operation.cg.bean.CgSupplier;
import com.jy.adscm.operation.cg.mapper.mysql.CgMaterialMapper;
import com.jy.adscm.operation.cg.mapper.mysql.CgSupplierMapper;
import com.jy.adscm.operation.mscyb.bean.JqylMaterial;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 售后原料任务 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-08-12
 */
@Service
public class ShylMaterialServiceImpl extends ServiceImpl<ShylMaterialMapper, ShylMaterial> implements ShylMaterialService {
    @Autowired
    CgMaterialMapper cgMaterialMapper;
    @Autowired
    JyUserMapper jyUserMapper;

    @Autowired
    CgSupplierMapper cgSupplierMapper;
    @Override
    public Boolean updateShylMaterialy(ShylMaterial shylMaterial, Token token) {
        //待办修改
        if (shylMaterial.getTaskstate()==0 && shylMaterial.getTaskfinishstate()==0 &&
                "0".equals(shylMaterial.getIstaskstart()) &&  "0".equals(shylMaterial.getIstaskbegin()) &&
                "0".equals(shylMaterial.getIstaskend())  && "0".equals(shylMaterial.getIsreviewtask())
        ){

            return  super.updateById(shylMaterial);
        }
        //接收任务
        if (shylMaterial.getTaskstate()==0 && shylMaterial.getTaskfinishstate()==0 &&
                "1".equals(shylMaterial.getIstaskstart()) &&  "0".equals(shylMaterial.getIstaskbegin()) &&
                "0".equals(shylMaterial.getIstaskend())  && "0".equals(shylMaterial.getIsreviewtask())
        ){
            shylMaterial.setIstaskstart(1);
            shylMaterial.setTaskstartaddtime(new Date());
            shylMaterial.setTaskstate(1);
            return  super.updateById(shylMaterial);
        }
        //开始任务
        if (shylMaterial.getTaskstate()==1 && shylMaterial.getTaskfinishstate()==0 &&
                "1".equals(shylMaterial.getIstaskstart()) &&  "1".equals(shylMaterial.getIstaskbegin()) &&
                "0".equals(shylMaterial.getIstaskend())  && "0".equals(shylMaterial.getIsreviewtask())
        ){
            shylMaterial.setIstaskbegin(1);
            shylMaterial.setTaskbeginaddtime(new Date());
            shylMaterial.setRemainingnums(shylMaterial.getActualcompletenums());
            return super.updateById(shylMaterial);
        }
        //结束任务
        if (shylMaterial.getTaskstate()==1 && shylMaterial.getTaskfinishstate()==0 &&
                "1".equals(shylMaterial.getIstaskstart()) &&  "1".equals(shylMaterial.getIstaskbegin()) &&
                "1".equals(shylMaterial.getIstaskend())  && "0".equals(shylMaterial.getIsreviewtask())
        ){
            shylMaterial.setIstaskend(1);
            shylMaterial.setTaskfinishstate(1);
            shylMaterial.setTaskendaddtime(new Date());
            return super.updateById(shylMaterial);
        }
        //复核任务
        if (shylMaterial.getTaskstate()==1 && shylMaterial.getTaskfinishstate()==1 &&
                "1".equals(shylMaterial.getIstaskstart()) &&  "1".equals(shylMaterial.getIstaskbegin()) &&
                "1".equals(shylMaterial.getIstaskend())  && "1".equals(shylMaterial.getIsreviewtask())
        ){
            shylMaterial.setIsreviewtask(1);
            shylMaterial.setReviewaddtime(new Date());
            shylMaterial.setReviewperson(token.getUser().getId().toString());
            return super.updateById(shylMaterial);
        }

        return null;
    }

    @Override
    public Page<ShylMaterial> selectShylMaterial(Page<ShylMaterial> page, QueryShylMaterial queryShylMaterial) {
        Wrapper<ShylMaterial> wrapper =new EntityWrapper<>();
        wrapper.eq("TASKSTATE",queryShylMaterial.getTaskState());
        wrapper.eq("TASKFINISHSTATE",queryShylMaterial.getTaskFinishState());
        wrapper.eq("isReviewTask",queryShylMaterial.getIsReviewTask());
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryShylMaterial.getBom(),null)){
            wrapper.eq("RESOURCENAME",queryShylMaterial.getBom());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryShylMaterial.getSupplier(),null)){
            wrapper.eq("BUSINESSTYPE",queryShylMaterial.getSupplier());
        }
        page =super.selectPage(page, wrapper);
        List<ShylMaterial> shylMaterials = page.getRecords();
        for (ShylMaterial shylMaterial : shylMaterials) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(shylMaterial.getResourcename(),null)){
                CgMaterial cgMaterial = cgMaterialMapper.selectById(shylMaterial.getResourcename());
                if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial,null)){
                    shylMaterial.setResourcenameName(cgMaterial);
                }
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(shylMaterial.getBusinesstype(),null)){
                CgSupplier cgSupplier = cgSupplierMapper.selectById(shylMaterial.getBusinesstype());
                shylMaterial.setBusinesstypeName(cgSupplier.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(shylMaterial.getTaskreleaseperson(),null)){
                JyUser jyUser = jyUserMapper.selectById(shylMaterial.getTaskreleaseperson());
                shylMaterial.setTaskreleasepersonName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(shylMaterial.getTaskperformperson(),null)){
                JyUser jyUser = jyUserMapper.selectById(shylMaterial.getTaskperformperson());
                shylMaterial.setTaskperformpersonName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(shylMaterial.getReviewperson(),null)){
                JyUser jyUser = jyUserMapper.selectById(shylMaterial.getReviewperson());
                shylMaterial.setReviewpersonName(jyUser.getName());
            }
        }
        page.setTotal(super.selectCount(wrapper));
        return page;
    }

    @Override
    public ShylMaterial updateShylMaterialValue(String id, Integer value) throws AdscmSystemException {

        //只能有一个线程进入修改库存
        synchronized (this){
            ShylMaterial shylMaterial = super.selectById(id);
            if (!AdscmBaseUtils.getInstance().isNotEmpty(shylMaterial, null)) {
                throw new AdscmSystemException("未查询到该物料");
            }
            if (shylMaterial.getIstaskbegin()!=1) {
                throw new AdscmSystemException("物料未入库");
            }
            if (shylMaterial.getRemainingnums() == 0) {
                throw new AdscmSystemException("物料已经用完");
            }
            if ((shylMaterial.getRemainingnums()+value)<0) {
                throw new AdscmSystemException("使用量超过了剩余量");
            }
            shylMaterial.setRemainingnums(shylMaterial.getRemainingnums()+value);
            if ( shylMaterial.getRemainingnums()==0){
                shylMaterial.setIstaskend(0);
            }
            super.updateById(shylMaterial);
            return shylMaterial;
        }

    }

    @Override
    public List<ShylMaterial> selectInStock() {
        Wrapper<ShylMaterial> wrapper=new EntityWrapper<>();
         wrapper.eq("TASKSTATE",1);
        wrapper.eq("TASKFINISHSTATE",0);
        wrapper.eq("isReviewTask",0);
        List<ShylMaterial> shylMaterials = super.selectList(wrapper);
        for (ShylMaterial shylMaterial : shylMaterials) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(shylMaterial.getResourcename(),null)){
                CgMaterial cgMaterial = cgMaterialMapper.selectById(shylMaterial.getResourcename());
                if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial,null)){
                    shylMaterial.setResourcenameName(cgMaterial);
                }
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(shylMaterial.getBusinesstype(),null)){
                CgSupplier cgSupplier = cgSupplierMapper.selectById(shylMaterial.getBusinesstype());
                shylMaterial.setBusinesstypeName(cgSupplier.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(shylMaterial.getTaskreleaseperson(),null)){
                JyUser jyUser = jyUserMapper.selectById(shylMaterial.getTaskreleaseperson());
                shylMaterial.setTaskreleasepersonName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(shylMaterial.getTaskperformperson(),null)){
                JyUser jyUser = jyUserMapper.selectById(shylMaterial.getTaskperformperson());
                shylMaterial.setTaskperformpersonName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(shylMaterial.getReviewperson(),null)){
                JyUser jyUser = jyUserMapper.selectById(shylMaterial.getReviewperson());
                shylMaterial.setReviewpersonName(jyUser.getName());
            }
        }
        return shylMaterials;
    }
}
